/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.refactor;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.MultiLineLabelUI;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class MessageDialog extends DialogWrapper {
    private Logger logger = Logger.getInstance(getClass().getName());

    private String message;
    private String helpKey;
    private Icon icon;
    private boolean isCancellable;
    private String[] options;
    private JList jlist;
    private int selectionMode;

    public MessageDialog(String title, String message, String helpKey, String iconName, String[] options, boolean flag, Project project, int selectionMode) {
        super(project, false);
        this.selectionMode = selectionMode;
        setTitle(title);
        this.message = message;
        this.helpKey = helpKey;
        isCancellable = flag;
        this.options = options;
        setButtonsAlignment(0);
        icon = UIManager.getIcon(iconName);
        init();
    }

    protected Action[] createActions() {
        ArrayList arraylist = new ArrayList();
        arraylist.add(getOKAction());
        if (isCancellable)
            arraylist.add(getCancelAction());
        if (helpKey != null)
            arraylist.add(getHelpAction());
        return (Action[]) arraylist.toArray(new Action[arraylist.size()]);
    }

    protected JComponent createNorthPanel() {
        JLabel jlabel = new JLabel(message);
        jlabel.setUI(new MultiLineLabelUI());
        JPanel jpanel = new JPanel(new BorderLayout());
        jpanel.add(jlabel, "North");
        if (icon != null) {
            jlabel.setIcon(icon);
            jlabel.setIconTextGap(10);
        }
        if (options != null) {
            jlist = new JList(options);
            jlist.setSelectionMode(selectionMode);
            jpanel.add(new JScrollPane(jlist), "Center");
        }
        jpanel.add(Box.createVerticalStrut(7), "South");
        return jpanel;
    }

    public JComponent getPreferredFocusedComponent() {
        return jlist;
    }

    public String getSelectedListOption() {
        return (String) jlist.getSelectedValue();
    }

    protected JComponent createCenterPanel() {
        return null;
    }

    protected void doHelpAction() {
        HelpManager.getInstance().invokeHelp(helpKey);
    }

    public int[] getSelectedListIndexes() {
        return jlist.getSelectedIndices();
    }
}

